
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page language="java" import="java.sql.*" %>

<%


    Connection con = null;
    ResultSet rs = null;
    Statement stmt = null;

    try{
        String url = "jdbc:sqlserver://localhost:1433;databaseName=ReviewJSP";
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        con = DriverManager.getConnection(url, "sa", "fat123456");
        stmt = con.createStatement();
        System.out.println("Con Success");
    }catch(Exception e){
        System.out.print(e.getMessage());
    }

    if(request.getParameter("action") != null){
        String bookname = request.getParameter("bookname");
        String author = request.getParameter("author");
        stmt.executeUpdate("Insert into books_details(book_name, author)"
                + " values('"+bookname+"','"+author+"')");
        rs=stmt.executeQuery("select * from books_details");

        %>
        <html>
            <body>
                <center>
                    <h2>Books List</h2>
                    <table border="1" cellpadding="0" cellspacing="0">
                        <tr>
                            <td><b>S.No</b></td>
                            <td><b>Book Name</b></td>
                            <td><b>Author</b></td>
                        </tr>
            <%
                int no = 1;
                while(rs.next()){
            %>
            <tr>
                <td><%=no%></td>
                <td><%=rs.getString("book_name")%></td>
                <td><%=rs.getString("author")%></td>
            </tr>
            <%
            no++;
                }
                rs.close();
                stmt.close();
                con.close();
                %>

                    </table>
                </center>
            </body>
        </html>


   <% }else{%>
   <html>
       <head>
           <title>Book Entity FormDocument</title>
           <script type="text/javascript" language="javascript">
               function validate(obForm){
                   if(obForm.bookname.value.length==0){
                       alert("Please enter Book Name!");
                       obForm.bookname.focus();
                       return false;
                   }
                   if(obForm.author.value.length==0){
                       alert("Plese enter author Name!");
                       obForm.author.focus();
                       return false;
                   }
                   return true;
               }
           </script>
       </head>
       <body>
           <center>
               <form action="BookEntityForm.jsp" method="post"
                     name="entity" onSubmit="return validate(this)">
                   <input type="hidden" value="list" name="action">
                   <table border="1" cellpadding="0" cellspacing="0">
                       <tr>
                           <td>
                               <table>
                                   <tr>
                                       <td colspan="2" align="center">
                                           <h2>Book Entity Form</h2>
                                       </td>
                                   </tr>
                                    <tr>
                                       <td colspan="2" align="center">
                                           &nbsp;
                                       </td>
                                   </tr>
                                    <tr>
                                        <td>Book Name: </td>
                                       <td>
                                           <input type="text" name="bookname" size="50">
                                       </td>
                                   </tr>
                                    <tr>
                                        <td>Author: </td>
                                       <td>
                                           <input type="text" name="author" size="50">
                                       </td>
                                   </tr>
                                    <tr>
                                       <td colspan="2" align="center">
                                           <input type="submit" value="submit">
                                       </td>
                                   </tr>
                               </table>
                           </td>
                       </tr>
                   </table>
               </form>
           </center>
       </body>
   </html>
   <%}%>

